package com.tim.curl;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

/**
 Copy as cURL 自动转成 Java OkHttp 代码：
    https://curlconverter.com/

 * */
public class TianTianJJW {


    //fetch("http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery1830945948424483898_1755519620712&fundCode=014938&pageIndex=1&pageSize=20&startDate=2023-08-01&endDate=2025-08-18&_=1755520544091", {
    //  "headers": {
    //    "accept": "*/*",
    //    "accept-language": "zh-CN,zh;q=0.9"
    //  },
    //  "referrer": "http://fundf10.eastmoney.com/",
    //  "referrerPolicy": "strict-origin-when-cross-origin",
    //  "body": null,
    //  "method": "GET",
    //  "mode": "cors",
    //  "credentials": "include"
    //});

    @Test
    public void parseFetch(){
        String str="fetch(\"http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery1830945948424483898_1755519620712&fundCode=014938&pageIndex=1&pageSize=20&startDate=2023-08-01&endDate=2025-08-18&_=1755520544091\", {\n" +
                "  \"headers\": {\n" +
                "    \"accept\": \"*/*\",\n" +
                "    \"accept-language\": \"zh-CN,zh;q=0.9\"\n" +
                "  },\n" +
                "  \"referrer\": \"http://fundf10.eastmoney.com/\",\n" +
                "  \"referrerPolicy\": \"strict-origin-when-cross-origin\",\n" +
                "  \"body\": null,\n" +
                "  \"method\": \"GET\",\n" +
                "  \"mode\": \"cors\",\n" +
                "  \"credentials\": \"include\"\n" +
                "});" +
                "";
        FetchObject fetchObject = getFetchObject(str);
        System.out.println(JSON.toJSONString(fetchObject));
    }

    @NotNull
    private static FetchObject getFetchObject(String str) {
        str = str.trim().replaceFirst("fetch\\(", "")
                .replaceAll("\\);","");
        int index = str.indexOf('{');
        String url= str.substring(0, index).trim();
        url=url.substring(1,url.length()-1);
        String jsonStr= str.substring(index);
        FetchObject fetchObject = JSON.parseObject(jsonStr, FetchObject.class);
        fetchObject.setUrl(url);
        return fetchObject;
    }

    public static void main(String[] args) throws IOException {
        String cookie="";
        String fetchCmd="fetch(\"http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery1830945948424483898_1755519620712&fundCode=014938&pageIndex=1&pageSize=20&startDate=2023-08-01&endDate=2025-08-18&_="+System.currentTimeMillis()+"\", {\n" +
                "  \"headers\": {\n" +
                "    \"accept\": \"*/*\",\n" +
                "    \"accept-language\": \"zh-CN,zh;q=0.9\"\n" +
                "  },\n" +
                "  \"referrer\": \"http://fundf10.eastmoney.com/\",\n" +
                "  \"referrerPolicy\": \"strict-origin-when-cross-origin\",\n" +
                "  \"body\": null,\n" +
                "  \"method\": \"GET\",\n" +
                "  \"mode\": \"cors\",\n" +
                "  \"credentials\": \"include\"\n" +
                "});" +
                "";
        FetchObject fetchObject = getFetchObject(fetchCmd);

        // 构建请求
        Request.Builder builder = new Request.Builder()
                .url(fetchObject.getUrl());
        builder=builder.method(fetchObject.getMethod(), fetchObject.getRequestBody());
        Map<String, String> headersMap = fetchObject.getHeadersMap();
        headersMap.put("cookie", cookie);
        for(String key : headersMap.keySet()) {
            builder=builder.addHeader(key, headersMap.get(key));
        }
        Request request = builder.build();

        // 创建 OkHttpClient
        OkHttpClient client = new OkHttpClient();
        // 发送请求
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                String responseBody = response.body().string();
                System.out.println(responseBody);
            } else {
                System.err.println("请求失败: " + response.code());
            }
        }
    }
}
